package com.fare.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {

	public static String tranferDate(int amount) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, amount);
		Date date =calendar.getTime();
		String dateStr = DateUtils.dateToStr(date,"yyyy-mm-dd");
		return dateStr;
	}
	
	public static String getTimeShort() {

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");

		Date currentTime = new Date();

		String dateString = formatter.format(currentTime);
		return dateString;
	}
	public static String getDateStr(String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		Date currentTime = new Date();
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	public static String dateToStr(Date d,String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(d);
		return dateString;
	}
	/**
	 * 字符串转成时间
	 * @param str
	 * @return
	 */
	 public static Date strToDate(String  str,String format){
		  SimpleDateFormat df = new SimpleDateFormat(format); 
		  Date date=null;
		try {
			date = df.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		} 
		  return date;
	  }
	/**
	 * 
	 * @param strTime
	 * @return 返回的时间必须为2013022301 10位
	 */
	public static String getPageDateStr(String strTime) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
		 try {
			Date date = df.parse(strTime);
			return dateToStr(date,"yyyyMMddHH");
		} catch (ParseException e) {
			e.printStackTrace();
		} 
		return null;
	}
	/**
	 * 
	 * @param strTime
	 * @return 返回的时间必须为2013022301 8位
	 */
	public static String getPageDateStr2(String strTime) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
		 try {
			Date date = df.parse(strTime);
			return dateToStr(date,"yyyyMMdd");
		} catch (ParseException e) {
			e.printStackTrace();
		} 
		return null;
	}
	/**
	 * 
	 * @param strTime
	 * @return 返回的时间必须为20130223 8位 ，返回没有小时的时间
	 */
	public static String getPageDateStr(String strTime,String format) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
		 try {
			Date date = df.parse(strTime);
			return dateToStr(date,format);
		} catch (ParseException e) {
			e.printStackTrace();
		} 
		return null;
	}
	  //   给定一个日期，返回加减n天后的日期 
	  /**
	   * 给定一个日期，返回加减n天后的日期  
	   * 如果是正数则是N天后的日期，如果是负数则，N天前的日期。如果今天是7号-3则是4号
	   */
    @SuppressWarnings("null")
	public  static Date   nDaysAfterOneDate(Date  basicDate,Integer   n)   {
    	Date date=new Date();
        long   nDay=(basicDate.getTime()/(24*60*60*1000)+n)*(24*60*60*1000);  
        date.setTime(nDay);  
        return   date;  
    }  
    /** 返回时间差
	   * @param endTime 
	   * @param beginTime
	   * @return   endTime-beginTime  返回s
	   */
	  public static long intervalTime(Date endTime,Date beginTime){
		  if(endTime==null||beginTime==null)
			  return 1L;
		  try{
			  return  (endTime.getTime()-beginTime.getTime())/1000;//除以1000转换成秒 
		  }catch(Exception e){
			  return 1L;
		  }
	  }
	  public static List<String> getDatesStr(final String startDay,String endDay){
		    final Date startD=strToDate(startDay+" 00:00","yyyy-MM-dd hh:mm");
		    final Date endD=strToDate(endDay+" 23:59","yyyy-MM-dd hh:mm");
		  Long n=(long) Math.ceil(intervalTime(endD,startD)/(24*60*60.0));
		  List list=new ArrayList();
		  for (int i = 0; i < n; i++) {
			  list.add(dateToStr(nDaysAfterOneDate(startD,(i+1)),"yyyy-MM-dd"));
		}
		  System.out.println(list);
		  return list;
		  
	  }
	  public static String getNextDateByDateStr(String datestr,int next,String format) throws Exception{
			Calendar calendar = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			calendar.setTime(sdf.parse(datestr));
			calendar.add(Calendar.DATE,next);
			return sdf.format(calendar.getTime());
		}
		public static String getNextDateByDateStr(String datestr,String format) throws Exception{
			return getNextDateByDateStr(datestr,1,format);
		}
	public static void main(String[] args) {
		getDatesStr("2013-01-23","2013-01-23");
		Date endDate45=DateUtils.nDaysAfterOneDate(new Date(), -30);
		System.out.println(dateToStr(endDate45,"yyyy-MM-dd"));
		List<String > dates=DateUtils.getDatesStr(DateUtils.dateToStr(endDate45,"yyyy-MM-dd"),"2013-03-09");
		for (String s : dates) {
			System.err.println(s);
		}
	}
}
